package com.sense.kukoo.common.wiget.dialog;

import android.animation.Animator;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.widget.TextView;

import androidx.annotation.ColorInt;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.StringRes;

import com.sense.kukoo.common.R;
import com.sense.kukoo.common.utils.ApplicationContext;

public class BottomConfigDialog extends Dialog {

    private TextView btn_1;
    private TextView btn_2;
    private TextView tv_msg;
    private int btn_1_str;
    private int btn_1_color;
    private int btn_2_str;
    private int btn_2_color;
    private int hint_str;
    private View root;

    public BottomConfigDialog(@NonNull Context context) {
        super(context);
    }

    public BottomConfigDialog(@NonNull Context context, int themeResId) {
        super(context, themeResId);
    }

    protected BottomConfigDialog(@NonNull Context context, boolean cancelable, @Nullable OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getContext().setTheme(R.style.bottom_dialog);
        setContentView(R.layout.common_bottom_config_dialog);
        //
        root = findViewById(R.id.root);
        btn_1 = findViewById(R.id.btn_bottom_1);
        btn_2 = findViewById(R.id.btn_bottom_2);
        tv_msg = findViewById(R.id.tv_msg);

        root.setOnClickListener(mOnClickListener);
        btn_1.setOnClickListener(mOnClickListener);
        btn_2.setOnClickListener(mOnClickListener);
        //
        btn_1.setText(btn_1_str);
        btn_1.setTextColor(btn_1_color);
        btn_2.setText(btn_2_str);
        btn_2.setTextColor(btn_2_color);
        tv_msg.setText(hint_str);
    }

    public BottomConfigDialog setBtn_1(@StringRes int resid, @ColorInt int color){
        this.btn_1_str = resid;
        this.btn_1_color = color;
        return this;
    }

    public BottomConfigDialog setBtn_2(@StringRes int resid, @ColorInt int color){
        this.btn_2_str = resid;
        this.btn_2_color = color;
        return this;
    }

    public BottomConfigDialog setHintMsg(@StringRes int resid){
        this.hint_str = resid;
        return this;
    }

    @Override
    public void show() {
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS
                | WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
        super.show();
        root.setTranslationY(ApplicationContext.dp2px(250));
        root.animate().translationY(0).setDuration(200).start();
    }

    @Override
    public void dismiss() {
        root.animate()
                .translationY(ApplicationContext.dp2px(250))
                .setDuration(200)
                .withEndAction(new Runnable() {
                    @Override
                    public void run() {
                        _dismiss();
                    }
                })
                .start();
    }

    private void _dismiss(){
        super.dismiss();
    }

    private BottomConfigListener mListener;

    public interface BottomConfigListener {

        void onButtonDownClick();

        void onButtonUpClick();
    }

    private final View.OnClickListener mOnClickListener = new View.OnClickListener(){

        @Override
        public void onClick(View v) {
            int id = v.getId();
            if (id == R.id.root) {
                dismiss();
            }else if(id == R.id.btn_bottom_1){
                if (mListener != null) {
                    mListener.onButtonDownClick();
                }
            }else if(id == R.id.btn_bottom_2){
                if (mListener != null) {
                    mListener.onButtonUpClick();
                }
            }
        }
    };

    public BottomConfigDialog setListener(BottomConfigListener listener){
        mListener = listener;
        return this;
    }
}
